package model

import (
	"newRetailByGolang/variable"
	"time"
)

type UpgradeRecord struct {
	Id int64
	UpgradeType int
	UserId int64
	UserName string
	RegistrationActivitieId int64
	RegistrationActivitieName string
	IsCounted int
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (upgradeRecord *UpgradeRecord) FindUpgrade(startTime time.Time, endTime time.Time, upgradeType int, userId int64, supUserId int64, activityId int64) []*UpgradeRecord {

	var upgradeList []*UpgradeRecord

	//variable.Db.Table("upgrade_records").
	//	Where("created_at >= ?", startTime).
	//	Where("created_at <= ?", endTime).
	//	Where("upgrade_type = ?", upgradeType).
	//	Where("user_id = ?", userId).
	//	Where("is_counted = ?", 1).
	//	Find(&upgradeList)

	variable.Db.Raw("select * from upgrade_records where created_at >= ? and created_at <= ? and upgrade_type = ? and user_id = ? and is_counted = 1 \n" +
		"and id not in (select task_records.upgrade_record_id from task_records where task_records.user_id = ? and task_records.registration_activitie_id = ?)",
		startTime, endTime, upgradeType, userId, supUserId, activityId).Find(&upgradeList)

	return upgradeList
}

func (upgradeRecord *UpgradeRecord) UpdateUpgrade(id int64) {
	var ur UpgradeRecord
	variable.Db.Model(&ur).
		Where("id = ?", id).
		Update("is_counted", 2)
}